<?php

namespace app\zyhsapi\controller\v2;

use app\common\lib\Util;
use app\zyhsapi\controller\ZyhsAuth;
use app\zyhsapi\model\ZyhsAppFeedback;
use app\zyhsapi\model\ZyhsConsultChatRtcRecord;
use app\zyhsapi\model\ZyhsUserQuestion;
use think\response\Json;

class Feedback extends ZyhsAuth
{
    /**
     * 获取 反馈历史
     * @return Json
     * @throws \Exception
     */
    public function getFeedbackList()
    {
        $minId = input('min_id/d', 0);

        $zyhsAppFeedbackModel = new ZyhsAppFeedback();
        if ($minId === 0 && $zyhsAppFeedbackModel->getHasUnread(1,$this->user['id']))
        {   // 有未读消息 更新至已读
            $zyhsAppFeedbackModel->msgRead(1,$this->user['id']);
        }

        $data = $zyhsAppFeedbackModel->getFeedbackList(1,$this->user, $minId);

        return show(config('code.success'), '请求成功', $data);
    }

    /**
     * 发送反馈消息
     * @return Json
     * @throws \Exception
     */
    public function addFeedbackMsg()
    {
        $content = trim(input('content/s', ''));   // 反馈内容
        $type = input('type/d', 1);
        $phoneInfo = input('phone_info/s', '');
        if (empty($content))
        {
            return show(config('code.error'), '请求异常，反馈内容不能为空～');
        }
        if (!in_array($type, [1, 2, 3, 4], true))
        {
            return show(config('code.error'), '请求异常，反馈类型错误～');
        }
        if (empty($phoneInfo))
        {
            return show(config('code.error'), '请求异常，设备信息不能为空～');
        }

        if (Util::commentCheck($this->user['id'], ['action' => 'addFeedbackMsg', 'type' => $type, 'content' => $content]))
        {
            return show(config('code.error'), '操作频繁，请稍后再试～');
        }
        $extend = [];
        if (in_array($type, [3, 4], true))
        {   // 反馈 服务
            if ($type === 3)
            {   // 连线咨询
                $data = (new ZyhsConsultChatRtcRecord())->getInfoById($content);
                $serType = 1;
            } else
            {   // 悬赏提问
                $data = (new ZyhsUserQuestion())->getInfoById($content);
                $serType = 2;
            }
            if (!$data || $data['uid'] !== $this->user['id'])
            {
                return show(config('code.error'), '服务异常，反馈服务不存在～');
            }
            $extend = [
                'type' => $serType, // 1=>连线咨询 2=>悬赏问答
                'id' => (int)$content,
            ];
            $content = '';
        }

        $data = [
            'app_type' => 1,
            'uid' => $this->user['id'],
            'type' => 1,
            'content' => $content,
            'content_type' => $type,
            'extend' => $extend,
            'app_version' => $this->headers['version'],
            'phone_type' => $this->headers['model'],
            'phone_system' => $this->headers['app-type'] === 'ios' ? 2 : 1,
            'phone_info' => $phoneInfo,
            'is_read' => 0,
            'read_time' => 0,
            'create_time' => $this->request->time(),
        ];

        $zyhsAppFeedbackModel = new ZyhsAppFeedback();
        $id = $zyhsAppFeedbackModel->SaveData($data);
        $data['id'] = $id;
        $resData = current($zyhsAppFeedbackModel->resDataResert([$data], $this->user));
        $resData['time_des'] = '';

        return show(config('code.success'), '请求成功', $resData);
    }
}
